import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:get/get.dart';
import 'package:note/common/common.dart';
import 'package:note/common/tools.dart';
import 'package:note/routeConfig.dart';

import '../manager/global.dart';
import '../models/noteModel.dart';

class NoteCard extends StatelessWidget {
  const NoteCard(this.note, {super.key});

  final Note note;

  void onClick() {
    if (!note.isLocked) {
      Get.toNamed(RouteConfig.noteInfoPage, arguments: note);
    } else {
      Get.toNamed(RouteConfig.unlockPage)?.then((value) {
        if (value) {
          Toasts.showToast(Strings.unlockSuccess);
          Get.toNamed(RouteConfig.noteInfoPage, arguments: note);
        } else {
          Toasts.showToast(Strings.unlockFail);
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Slidable(
      // key: Key(UniqueKey().toString()),
      groupTag: 'noteCard',
      endActionPane: ActionPane(
        extentRatio: 0.3,
        motion: const DrawerMotion(),
        children: [
          SlidableAction(
            // label: 'Favorite',
            backgroundColor: Styles.backgroundColor,
            foregroundColor: Styles.favorite,
            icon: note.isFavorite ? Icons.favorite : Icons.favorite_border,
            onPressed: (context) {
              note.isFavorite = (!note.isFavorite);
              if (note.isFavorite) {
                GlobalUtil.favoriteNum.value++;
              } else {
                GlobalUtil.favoriteNum.value--;
              }
              GlobalUtil.setNote(note);
            },
          ),
          SlidableAction(
            // label: 'Delete',
            backgroundColor: Colors.red,
            icon: Icons.delete_forever,
            onPressed: (context) {
              GlobalUtil.deleteNote(note);
            },
          ),
        ],
        // dismissible: Icon(Icons.delete),
      ),
      child: GestureDetector(
        onTap: onClick,
        child: Container(
          margin:
              const EdgeInsets.only(left: 20, right: 20, top: 10, bottom: 10),
          padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
          decoration: const BoxDecoration(
              color: Styles.backgroundColor,
              borderRadius: BorderRadius.all(Radius.circular(15)),
              boxShadow: [
                BoxShadow(
                    color: Styles.shadowColor,
                    blurRadius: 4,
                    offset: Offset(2, 2))
              ]),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                flex: 1,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const SizedBox(
                      height: 15,
                    ),
                    Text(
                      note.title,
                      style: Styles.textStyleMedium(20),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(
                      height: 5,
                    ),
                    Row(
                      children: [
                        Text(
                          DateTimeToString.toYYMMDDHSN(note.editTime),
                          style: Styles.textStyleNormal(15)
                              .copyWith(color: Styles.textTranslucentColor),
                        ),
                        const SizedBox(
                          width: 5,
                        ),
                        Visibility(
                          visible: note.isFavorite,
                          child: const Icon(
                            Icons.favorite,
                            color: Styles.favorite,
                            size: 16,
                          ),
                        )
                      ],
                    ),
                    const SizedBox(
                      height: 15,
                    ),
                  ],
                ),
              ),
              note.isLocked
                  ? const Icon(
                      Icons.lock_outline,
                      color: Styles.mainThemeColor,
                    )
                  : Visibility(
                      visible: note.images.isNotEmpty,
                      child: SizedBox(
                        width: 60,
                        height: 60,
                        child: ClipRRect(
                          borderRadius: const BorderRadius.all(
                            Radius.circular(15),
                          ),
                          child: note.images.isNotEmpty
                              ? Image(
                                  image: FileImage(File(note.images.first)),
                                  fit: BoxFit.cover,
                                )
                              : null,
                        ),
                      ),
                    ),
            ],
          ),
        ),
      ),
    );
  }
}
